//
//  DECXUploadIPictureItem.m
//  DECXInStore
//
//  Created by 程强 on 15/12/9.
//  Copyright © 2015年 程强. All rights reserved.
//

#import "DECXUploadIPictureItem.h"

@interface DECXUploadIPictureItem()
@property (strong, nonatomic) DECXManager *manager;
@property (strong, nonatomic) DSADisposable *dispose;
@end

@implementation DECXUploadIPictureItem
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self addPictureAndBtn];
        self.manager = [DECXManager new];
    }
    return self;
}
- (void)addPictureAndBtn
{
    self.picture = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    _picture.image =[UIImage imageNamed:@"shangchuan"];
    self.picture.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:_picture];
    
    self.logo = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width-70, 0, 70, 70)];
//    [self.picture addSubview:_logo];
    
    self.actionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_actionBtn setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [_actionBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_actionBtn];
    
    
    self.activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [_activityIndicator setFrame:CGRectMake(0, 0, 30, 30)];
    _activityIndicator.center = CGPointMake(self.frame.size.width/2.0, self.frame.size.height/2.0);
//    [self addSubview:_activityIndicator];
}
- (void)click:(UIButton *)btn
{
    UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从手机相册选择", nil];
    [sheet showInView:self];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        if (self.selectMediaBlock) {
            self.selectMediaBlock(MediaCameraType);
        }
    }else if (buttonIndex == 1){
        if (self.selectMediaBlock) {
            self.selectMediaBlock(MediaALAssetType);
        }
    }
}
- (void)startUpload:(UIImage *)oriImage thumImage:(UIImage *)thumImage success:(void(^)(DECXPictureFile *file))successBlock
            failure:(void(^)(NSString *msg))failureBlock complete:(void(^)())completeBlock
{
//    [self.activityIndicator startAnimating];
    //上传图片文件
    weak_Self(self);
    BOOL prove;
    if (self.pictureType == UploadPictureControllerTypeProductOwner) {
        prove = YES;
    }else
        prove = NO;
    
    if (self.dispose) {
        [self.dispose dispose];
        self.dispose = nil;
    }
    self.dispose = [[self.manager uploadFile:oriImage thumbnail:thumImage prove:prove]subscribeNext:^(id x) {
        DECXPictureFile *file = x;
        if (prove) {
            [DECXGlobal shareInstance].productProve = oriImage;
        }else{
            [DECXGlobal shareInstance].productLogo = oriImage;
        }
        successBlock(file);
    } error:^(NSError *error) {
        NSString *msg = error.userInfo[@"msg"];
        [weakSelf uploadFailed];
        failureBlock(msg);
    } completed:^{
        completeBlock();
    }];
}
- (void)uploadFailed
{
    self.picture.image = [UIImage imageNamed:@"shibai"];
}
- (void)dealloc
{
    if (self.dispose) {
        [self.dispose dispose];
        self.dispose = nil;
    }
}
@end
